package com.cyber.parkingremotev2;

import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Resources;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.TextView;

public class ParkRemoteActivity extends Activity {

	public static final int M_UPDATE_TEXT = 0;
	public static final String M_TEXT = "text";
	public static final int M_UPDATE_MOVING_STATE = 1;
	public static final String M_MOVING = "moving";
	public static final String M_RESTARTED = "restarted";
	public static final String M_LOOP = "loop";
	public static final int M_GOT_DATA = 2;
	public static final int M_STATUS = 3;// for status
	public static final String M_STATUS_INFO = "status";
	public static final String M_STATUS_SEQ = "status_seq";
	public static final String M_STATUS_DISTANCE = "status_distance";
	public static final int M_MODE = 4;// for mode
	public static final String M_MODE_INFO = "mode";
	public static final int M_EXCEPTION = 5;// for exception
	public static final String M_EXCEPTION_INFO = "exception";
	public static final int M_CONNECTION = 6;// for connection
	public static final String M_CONNECTION_INFO = "connection";

	TextView mTextInfo;
	TextView mTextMode;
	TextView mTextConnection;
	TextView mTextException;
	Button mButtonMove;
	Spinner mSpinnerTarget;
	Button mButtonConnect;
	CheckBox mCheckAuto;
	Button mButtonAuto;
	boolean mMoving;
	boolean mModeAuto;
	boolean mShowException;
	Button mButtonRestart;
	Button mButtonContinue;
	Communication.ComBinder mBinder;
	CommandSystem mCommandSystem;
	Resources mResource;
	int tempI = 0;

	// WifiService mWifiService;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mResource = getResources();
		mMoving = false;
		mModeAuto = false;
		mShowException = false;
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		setContentView(R.layout.activity_main);
		mCommandSystem = new CommandSystem();
		mTextInfo = (TextView) findViewById(R.id.textView1);
		mTextMode = (TextView) findViewById(R.id.textView2);
		mTextConnection = (TextView) findViewById(R.id.textView3);
		mTextException = (TextView) findViewById(R.id.textView4);
		mBinder = null;
		mSpinnerTarget = (Spinner) findViewById(R.id.spinner1);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.car_list, android.R.layout.simple_spinner_item);
		mSpinnerTarget.setAdapter(adapter);
		mButtonConnect = (Button) findViewById(R.id.button2);
		mButtonConnect.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				int id = mSpinnerTarget.getSelectedItemPosition();
				// String target = mSpinnerTarget.getSelectedItem().toString();
				String[] address = mResource
						.getStringArray(R.array.car_address);
				String target = address[id];
				connect(target);
			}

		});
		mButtonAuto = (Button) findViewById(R.id.checkBox1);
		mButtonAuto.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				loop(!mModeAuto);
			}

		});
		mButtonMove = (Button) findViewById(R.id.button1);
		mButtonMove.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				if (!mModeAuto) {
					switch (arg1.getAction()) {
					case MotionEvent.ACTION_DOWN:
						move();
						break;
					case MotionEvent.ACTION_UP: {
						pause();
					}
						break;
					default:
						break;
					}
				} else {
					switch (arg1.getAction()) {
					case MotionEvent.ACTION_DOWN:
						if (mMoving) {
							pause();
						} else {
							move();
						}
						break;
					default:
						break;
					}
				}
				return false;
			}
		});
		mButtonRestart = (Button) findViewById(R.id.button3);
		mButtonRestart.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				loop(false);

				restart();
			}
		});
		mButtonContinue = (Button) findViewById(R.id.button4);
		mButtonContinue.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				nextStep();
			}
		});
	}

	protected void onStart() {
		super.onStart();
		Intent bindIntent = new Intent(this, NetCommunication.class);
		mConnection = new ServiceConnection() {

			@Override
			public void onServiceConnected(ComponentName arg0, IBinder arg1) {
				mBinder = (Communication.ComBinder) arg1;
				mBinder.setUiHandler(ParkRemoteActivity.this.mUiHandler);
			}

			@Override
			public void onServiceDisconnected(ComponentName arg0) {
				mBinder = null;

			}

		};
		bindService(bindIntent, mConnection, Context.BIND_AUTO_CREATE);
	}

	protected void onResume() {
		super.onResume();
		Intent bindIntent = new Intent(this, NetCommunication.class);
		mConnection = new ServiceConnection() {

			@Override
			public void onServiceConnected(ComponentName arg0, IBinder arg1) {
				mBinder = (Communication.ComBinder) arg1;
				mBinder.setUiHandler(ParkRemoteActivity.this.mUiHandler);
			}

			@Override
			public void onServiceDisconnected(ComponentName arg0) {
				mBinder = null;

			}

		};
		bindService(bindIntent, mConnection, Context.BIND_AUTO_CREATE);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);

		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_show_exception:
			mShowException = true;
			return true;
		case R.id.menu_close_exception:
			mShowException = false;
			mTextException.setText("");
			return true;
		case R.id.menu_adjust:
			Intent intent = new Intent(this, AdjustActivity.class);
			startActivity(intent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}

	}

	void connect(String target) {
		if (mBinder != null)
			mBinder.connect(target);
	}

	void send(String data) {
		if (mBinder != null)
			mBinder.sendData(data);
	}

	// for logical
	void move() {
		send(mCommandSystem.move());
	}

	void pause() {
		send(mCommandSystem.pause());
	}

	void restart() {
		send(mCommandSystem.restart());
	}

	void loop(boolean state) {
		send(mCommandSystem.loop(state));
	}

	void nextStep() {
		send(mCommandSystem.nextStep());
	}

	private final Handler mUiHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			mTextException.setText("");
			switch (msg.what) {
			// case M_UPDATE_TEXT: {
			// String content = msg.getData().getString(M_TEXT);
			// mTextInfo.setText(content);
			// }
			// break;
			// case M_UPDATE_MOVING_STATE: {
			// mMoving = msg.getData().getBoolean(M_MOVING);
			// mTextInfo.setText(String.valueOf(mMoving));
			// }
			// break;
			// case M_GOT_DATA: {
			// String content = msg.getData().getString(M_TEXT);
			// mTextInfo.setText(content);
			// }
			// break;
			case M_STATUS: {
				String content = msg.getData().getString(M_STATUS_INFO);
				mTextInfo.setText(content);
				mMoving = content.equals(getString(R.string.status_parking));

			}
				break;
			case M_MODE: {
				String content = msg.getData().getString(M_MODE_INFO);
				mTextMode.setText(content);
				mModeAuto = content.equals(getString(R.string.mode_auto));
			}
				break;
			case M_CONNECTION: {
				String content = msg.getData().getString(M_CONNECTION_INFO);
				mTextConnection.setText(content);
				if (content.equals(getString(R.string.connected))) {
					loop(mModeAuto);
					pause();
					pause();
				}
			}
				break;
			case M_EXCEPTION: {
				if (mShowException) {
					String content = msg.getData().getString(M_EXCEPTION_INFO);
					mTextException.setText(content);
				} else {

				}

			}
				break;
			default:
				break;
			}
		}

	};
	private ServiceConnection mConnection;
	// private ServiceConnection mConnection = new ServiceConnection() {
	//
	// @Override
	// public void onServiceConnected(ComponentName arg0, IBinder arg1) {
	// mBinder = (Communication.ComBinder) arg1;
	// mBinder.setUiHandler(ParkRemoteActivity.this.mUiHandler);
	// }
	//
	// @Override
	// public void onServiceDisconnected(ComponentName arg0) {
	// mBinder = null;
	//
	// }
	//
	// };

}
